using UnityEngine;

public class AnimatorCurve : MonoBehaviour
{
	public AnimationCurveClip clip;

	[Tooltip("Воспроизвести при старте")]
	public bool playStart = true;

	[Tooltip("Проигрывать повторно")]
	public bool loop = true;

	[Tooltip("Множитель скорости анимаций")]
	public float timeScale = 1f;

	[Tooltip("Имя текущей анимации")]
	public string currentAnimation;

	private float frameTime;

	public SpriteRenderer[] objects = new SpriteRenderer[0];

	[HideInInspector]
	public bool isPlaying;

	[HideInInspector]
	public int startFrameClip;

	[HideInInspector]
	public int lastFrameClip;

	private int d;

	private int z;

	private float timeFrame;

	public int currentFrame => Mathf.FloorToInt(frameTime) - startFrameClip;

	public int totalFrames => lastFrameClip - startFrameClip;

	public int frameTimeline => Mathf.FloorToInt(frameTime);

	private void Awake()
	{
		isPlaying = playStart;
		if (string.IsNullOrEmpty(currentAnimation))
		{
			ResetAnimationRange(0);
		}
		else
		{
			SwitchAnimation(currentAnimation);
		}
	}

	private void Start()
	{
	}

	private void Update()
	{
		if (isPlaying)
		{
			Update(Time.deltaTime);
		}
	}

	public void Update(float deltaTime)
	{
		frameTime += (float)clip.frameRate * deltaTime * timeScale;
		if (frameTime < (float)lastFrameClip)
		{
			SetFrame(Mathf.FloorToInt(frameTime));
			return;
		}
		if (loop)
		{
			frameTime = SetFrame(startFrameClip);
			return;
		}
		Stop();
		frameTime = SetFrame(lastFrameClip);
	}

	public void SetRenderActive(bool value)
	{
		for (d = 0; d < objects.Length; d++)
		{
			if (objects[d].gameObject != base.gameObject)
			{
				objects[d].gameObject.SetActive(value);
			}
		}
	}

	public void Play()
	{
		playStart = true;
		isPlaying = (lastFrameClip >= 2);
	}

	public void Stop()
	{
		playStart = false;
		isPlaying = false;
	}

	public void GotoAndStop(int aFrame)
	{
		frameTime = SetFrame(aFrame + startFrameClip);
		Stop();
	}

	public void GotoAndPlay(int aFrame)
	{
		frameTime = SetFrame(aFrame + startFrameClip);
		Play();
	}

	public void SwitchClip(AnimationCurveClip clip, int frame)
	{
		this.clip = clip;
		ResetAnimationRange(frame);
	}

	public void SwitchClip(AnimationCurveClip clip, string animation, int frame)
	{
		this.clip = clip;
		SwitchAnimation(animation);
	}

	public void SwitchAnimation(string keyframe)
	{
		startFrameClip = clip.GetIdFrame(keyframe);
		lastFrameClip = clip.GetIdLastFrame(keyframe);
		currentAnimation = keyframe;
		frameTime = SetFrame(startFrameClip);
		if (isPlaying)
		{
			Play();
		}
		else
		{
			Stop();
		}
	}

	public void ResetAnimationRange(int frame)
	{
		startFrameClip = 0;
		lastFrameClip = clip.totalFrames;
		currentAnimation = string.Empty;
		frameTime = SetFrame(frame);
	}

	public int SetFrame(int frame)
	{
		frame = Mathf.Clamp(frame, startFrameClip, lastFrameClip);
		timeFrame = frame;
		timeFrame /= clip.frameRate;
		timeFrame = Mathf.Clamp(timeFrame, 0f, clip.totalTime);
		for (d = 0; d < objects.Length; d++)
		{
			for (z = 0; z < clip.layers.Length; z++)
			{
				if (objects[d].name == clip.layers[z].name)
				{
					if (objects[d].gameObject != base.gameObject)
					{
						objects[d].gameObject.SetActive(clip.layers[z].frames[0].frame <= frame && frame <= clip.layers[z].frames[clip.layers[z].frames.Length - 1].frame);
					}
					objects[d].transform.localRotation = Quaternion.Euler(0f, 0f, clip.layers[z].curveRotation.Evaluate(timeFrame));
					objects[d].transform.localPosition = new Vector3(clip.layers[z].curveX.Evaluate(timeFrame), clip.layers[z].curveY.Evaluate(timeFrame));
					objects[d].sprite = clip.GetLibrarySprite(z, frame);
					break;
				}
				if (z == clip.layers.Length - 1 && objects[d].gameObject.activeSelf)
				{
					objects[d].gameObject.SetActive(value: false);
				}
			}
		}
		return frame;
	}
}
